package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.result.ResultUtil;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.common.util.StringUtils;
import com.ctcemti.mdm.project.sys.entity.OrgStation;
import com.ctcemti.mdm.project.sys.entity.formdata.BaseReq;
import com.ctcemti.mdm.project.sys.service.IOrgStationService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@Api(value = "OrgStationController", description = "项目工点控制器类")
@RequestMapping("orgstation")
public class OrgStationController {
    @Autowired
    private IOrgStationService orgStationService;

    @ApiOperation(value = "分页获取项目工点数据list")
    @PostMapping("selectListByPid")
    @ApiImplicitParam(name="pid",value = "项目id")
    public Result selectListByPid(HttpServletRequest request, HttpServletResponse response, String pid, BaseReq baseReq){
        PageInfo pageInfo = orgStationService.selectListByPid(pid,baseReq);
        return new Result(ResultEnum.SUCCESS,pageInfo);
    }

    @ApiOperation(value = "保存项目工点数据")
    @PostMapping("insertOrgStation")
    @ApiImplicitParams({@ApiImplicitParam(name="entity",value = "项目工点数据实体类")})
    public Result<OrgStation> insertOrgStation(HttpServletRequest request, HttpServletResponse response , OrgStation entity){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        entity.setCreator(loginId);
        entity.setId(UUID.randomUUID().toString());
        orgStationService.insertSelective(entity);
        return new Result<OrgStation>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "更新项目工点数据")
    @PostMapping("updateOrgStation")
    @ApiImplicitParam(name="entity",value = "项目工点数据实体类")
    public Result<OrgStation> updateOrgStation(HttpServletRequest request, HttpServletResponse response, OrgStation entity){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        entity.setModifiedBy(loginId);
        entity.setModifyDate(new Date());
        orgStationService.updateByIdSelective(entity);
        return new Result<OrgStation>(ResultEnum .SUCCESS,null);
    }

    @ApiOperation(value = "删除项目工点数据")
    @PostMapping("deleteOrgStation")
    @ApiImplicitParam(name="ids",value = "项目工点ids")
    public Result<OrgStation> deleteOrgStation(HttpServletRequest request, HttpServletResponse response, String ids){
        orgStationService.deleteByPrimaryKeys(ids);
        return new Result<OrgStation>(ResultEnum.SUCCESS,null);
    }
    @ApiOperation(value = "更新项目工点所属项目")
    @PostMapping("moveOrgStation")
    @ApiImplicitParam(name="entity",value = "项目工点数据实体类")
    public Result<OrgStation> updateOrgStation(HttpServletRequest request, HttpServletResponse response, String ids,String projectId){
        orgStationService.moveOrgStation(ids,projectId,request);
        return new Result<OrgStation>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "根据项目工点数据项实体类获取list")
    @PostMapping("selectOrgStationByEntity")
    @ApiImplicitParam(name="entity",value = "项目工点项实体类")
    public Result<List<Map<String,Object>>> selectOrgStationByEntity(HttpServletRequest request, HttpServletResponse response,OrgStation entity){
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        list = orgStationService.selectOrgStation(entity);
        return new Result<List<Map<String,Object>>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("uploadFile")
    @ApiImplicitParam(name="projectId",value = "项目id")
    public Result<Map<String,Object>> uploadFile(HttpServletRequest request, HttpServletResponse response, String projectId) throws Exception {
        CommonsMultipartResolver commonsMultipartResolver =  new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartFile multipartFile = null;
        if(commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
            Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
            while (iterator.hasNext()){
                multipartFile= multipartHttpServletRequest.getFile(iterator.next());
            }
        }
        Map<String,Object> result = orgStationService.uploadFile(multipartFile,projectId,response,request);
        return new Result<Map<String,Object>>(ResultEnum.SUCCESS,result);
    }
    @ApiOperation(value = "获取项目节点")
    @PostMapping("getProjectsForTree")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value = "项目id"),
                        @ApiImplicitParam(name="type",value = "project类型"),
                        @ApiImplicitParam(name="name",value = "项目名称"),
                        @ApiImplicitParam(name="stationFlag",value = "是否展示工点（1为展示）")})
    public Result getProjectsForTree(HttpServletRequest request,String id,String type,String name,String stationFlag){
        String personId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        if(!StringUtil.isEmpty(name)){
            //stationFlag：1为包括工点，2为不包括工点
            return orgStationService.getProjectsForTreeByName(request,id,type,name,stationFlag);
        }
        if(StringUtils.isNullOrEmpty(id)){
            return ResultUtil.success();
        }
        if(StringUtils.isNullOrEmpty(type)){
            return ResultUtil.success();
        }
        if(StringUtils.isNullOrEmpty(personId)){
            return ResultUtil.success();
        }
        return orgStationService.getProjectsForTree(id,type,personId);
    }

}
